package br.com.webcrm.util;

import java.io.IOException;

import javax.el.MethodExpression;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;


public class UtilJSF {

	public static FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}
	
	public static HttpSession getHttpSession() {
		return (HttpSession) getFacesContext().getExternalContext().getSession(false);
	}

	public static void setMensagem(Severity severity, String titulo, String mensagem) {
		getFacesContext().addMessage(null, new FacesMessage(severity, titulo, mensagem));
	}
	
	public static Object getObjetoSessao(String nome) {
		return getHttpSession().getAttribute(nome);
	}
	
	public static void setObjetoSessao(String nome, Object objeto) {
		getHttpSession().setAttribute(nome, objeto);
	}
	
	public static void removerObjetoSessao(String nome) {
		getHttpSession().removeAttribute(nome);
	}
	
	public static void invalidarSessao() {
		getHttpSession().invalidate();
	}
	
	public static void redirecionar(String url) throws IOException {
		getFacesContext().getExternalContext().redirect(url);
	}
	
	public static void executarAcao(String acao) {
		MethodExpression methodEx = getFacesContext().getApplication().getExpressionFactory().createMethodExpression(FacesContext.getCurrentInstance().getELContext(), acao, String.class, new Class[] {});
		methodEx.invoke(getFacesContext().getELContext(), new Class[] {});
	}

}
